package com.briup.srms.cms.service.impl;

import com.briup.srms.cms.bean.Article;
import com.briup.srms.cms.bean.ArticleExample;
import com.briup.srms.cms.bean.extend.ArticleExtend;
import com.briup.srms.cms.dao.ArticleMapper;
import com.briup.srms.cms.dao.CategoryMapper;
import com.briup.srms.cms.dao.extend.ArticleExtendMapper;
import com.briup.srms.cms.service.IArticleService;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.PageVM;
import com.briup.srms.cms.bean.Category;
import com.briup.srms.cms.bean.CategoryExample;
import com.briup.srms.cms.vm.CateoryAriticleVM;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 文章的业务实现类
 * @author: charles
 * @create: 2020-11-12 10:21
 **/
@Service
public class ArticleServiceImpl implements IArticleService {

    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private ArticleExtendMapper articleExtendMapper;
    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public List<CateoryAriticleVM> queryCateoryAriticleVM() {
    	List<CateoryAriticleVM> list = new ArrayList<>();
    	List<Category> cs = categoryMapper.selectByExample(new CategoryExample());
    	for(Category c : cs) {
    		CateoryAriticleVM vm = new CateoryAriticleVM();
    		vm.setCategory(c);
    		List<ArticleExtend> as = articleExtendMapper.query(1, 10, null, null, null, c.getId());
    		vm.setArticles(as);
    		list.add(vm);
    	}
    	return list;
    }

    @Override
    public ArticleExtend findById(long id) {
        return articleExtendMapper.selectById(id);
    }

    @Override
    public void saveOrUpdate(Article article) throws CustomerException {
        if(article.getId()!=null){
        	Article dbArticle = articleMapper.selectByPrimaryKey(article.getId());
        	if(dbArticle == null) {
        		throw new CustomerException("要修改的数据不存在");
        	}
        	dbArticle.setTitle(article.getTitle());
        	dbArticle.setContent(article.getContent());
        	dbArticle.setCover(article.getCover());
        	dbArticle.setCategoryId(article.getCategoryId());
        	// 修改为最近更新时间
        	dbArticle.setPublishTime(new Date().getTime());
            articleMapper.updateByPrimaryKey(article);
        } else {
            // 标题不能重名
            ArticleExample example = new ArticleExample();
            example.createCriteria().andTitleEqualTo(article.getTitle());

            List<Article> articles = articleMapper.selectByExample(example);
            if(articles.size()>0){
                throw new CustomerException("标题不能重复");
            }
            // 初始化
            article.setPublishTime(new Date().getTime());
            article.setStatus(ArticleExtend.STATUS_UNCHECK);
            article.setThumpDown(0l);
            article.setThumpUp(0l);
            article.setReadTimes(0l);
            articleMapper.insert(article);
        }
    }

    @Override
	public PageVM<ArticleExtend> pageQuery(int page, int pageSize, String title, String status, Long authorId, Long categoryId) {
		List<ArticleExtend> list = articleExtendMapper.query(page, pageSize, title, status,authorId, categoryId);
		long total = articleExtendMapper.count(title, status,authorId, categoryId);
		return new PageVM<>(page, pageSize, total, list);
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		Article article = articleMapper.selectByPrimaryKey(id);
		if(article == null) {
			throw new CustomerException("要删除的数据不存在");
		}
		articleMapper.deleteByPrimaryKey(id);
	}
}
